/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Buscador.crawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import Buscador.GenerarMatriz;
import Buscador.OrdenInverso;
import Buscador.crawler.PagData;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author Alvaro
 */
public class Spider {

	public static int maxPag = 500;

	GenerarMatriz gm;
    OrdenInverso oi;

	ParserJericho pj;

	PagData pData;

	public void init(String url, int maxPag) {

		Spider.maxPag = maxPag;
        gm = new GenerarMatriz();
        oi = new OrdenInverso();
        pj = new ParserJericho();

        String pagina = descargaPagina(url);
        
		pData = pj.parsea(url, pagina);

		int numPag = gm.addPagina(pData.getUrl(), pData.getLinks());
        if (numPag > 0)
            oi.addPagina(numPag, pData.getPalabras());
		int i = 0;
		boolean morePages = true;

        System.out.println("Entrando al bucle");
		while (i < Spider.maxPag && morePages) {
            System.out.println("Pagina:: " +i);
			url = gm.getNuevaPagina();
			morePages = (url != null);


			if (morePages) {

				pagina = descargaPagina(url);
                if (pagina != null){
                    pData = pj.parsea(url, pagina);
                    numPag = gm.addPagina(pData.getUrl(), pData.getLinks());
                if (numPag > 0)
                    oi.addPagina(numPag, pData.getPalabras());
                }
			}

            i++;
            if ((i % 100) == 0) {
                try {
                    System.out.println("Sacando ficheros");
                    gm.generarMatrizAdy("MatrizAd.txt");
                    gm.generarFicheroNPag("FicheroPag.txt");
                    oi.sacaFich(new File("PalabraUrl.txt"));

                    try {
                        Process p = Runtime.getRuntime().exec("matlab -r \"run('PageRank')\" -nojvm -nosplash");
                    } catch (IOException ex) {
                        Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
                    }


                } catch (IOException iOException) {
                }
            }
        }
        System.out.println("Saliendo del bucle!");

        try {
            gm.generarMatrizAdy("MatrizAd.txt");
            gm.generarFicheroNPag("FicheroPag.txt");
            oi.sacaFich(new File("PalabraUrl.txt"));
        } catch (IOException iOException) {
        }
        try {
            Process p = Runtime.getRuntime().exec("matlab -r \"run('PageRank')\" -nojvm -nosplash");
        } catch (IOException ex) {
            Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
        }


	}

	/**
	 * @param direccion
	 *            Contiene la URL de la pagina
	 * 
	 * @return Contenido de la pagina y null si se produce un error y no se
	 *         puede descargar
	 */
	public String descargaPagina(String direccion) {

		String content = null;

		try {
			URL url = new URL(direccion);

			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			InputStream in = null;
			in = url.openStream();
			content = pipe(in, "utf-8");

		} catch (Exception e) {
			
		}

		return content;

	}

	private String pipe(InputStream in, String charset) throws IOException {
		StringBuffer s = new StringBuffer();
		if (charset == null || "".equals(charset)) {
			charset = "utf-8";
		}
		String rLine = null;
		BufferedReader bReader = new BufferedReader(new InputStreamReader(in,
				charset));
		/*
		 * PrintWriter pw = null;
		 * 
		 * FileOutputStream fo = new FileOutputStream("../index.html");
		 * OutputStreamWriter writer = new OutputStreamWriter(fo, "utf-8"); pw =
		 * new PrintWriter(writer);
		 */

		while ((rLine = bReader.readLine()) != null) {
			String tmp_rLine = rLine;
			int str_len = tmp_rLine.length();
			if (str_len > 0) {
				s.append(tmp_rLine);
				/*
				 * pw.println(tmp_rLine); pw.flush();
				 */
			}
			tmp_rLine = null;
		}
		in.close();
		/* pw.close(); */
		return s.toString();
	}

}
